// pages/world/world.js
import api from '../../utils/api';
import Toast from '@vant/weapp/toast/toast';
var util = require('../../utils/util.js');
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        citeInfo: [],
        citevalue: null,
        upTui: false,
        wxname: null,
        code: null,
        citeShow: false,
        weatherTime: null,
        weatherShow: false,
        weatherInfo: null,
        rollWeaterText: null,
        is_moonrise: false,
        is_rollWeaterText: false,
        TitleText: ['今天', '明天', '后天'],
        citeName: null
    },
    weatherTap() {
        this.setData({
            weatherShow: true
        })
    },
    onweaClose() {
        this.setData({
            weatherShow: false
        })
    },
    citeHide() {
        this.setData({
            citeShow: false
        })
    },
    onTuiChange() {
        this.setData({
            upTui: this.data.upTui ? false : true
        })
        console.log(this.data.wxname)
    },
    timeChange(e) {
        this.setData({
            wxname: e.detail
        })
    },
    submitTap() {
        if (this.data.code) {
            let val = {
                citecode: this.data.code
            }
            // console.log(this.data.upTui)
            if (this.data.upTui) {
                let vall = {
                    code: this.data.code,
                    citename: this.data.citevalue,
                    is_up: 0,
                    wxname: wx.getStorageInfoSync('nickname')
                }
                // 请求
                api.postsubmitCite(vall).then((res) => {
                    if (res.status == '0') {
                        // Toast('修改成功~');
                    } else {
                        // Toast('修改失败！');
                    }
                    // console.log(res)
                })
            }
            api.updateUserInfo(val).then((res) => {
                if (res.status == '0') {
                    Toast('修改成功~');
                    this.setData({
                        weatherShow: false
                    })
                } else {
                    Toast('修改失败！');
                }
            })
        } else {
            // 获取城市
            let values = {
                citename: this.data.citeName
            }
            api.postcitePlus(values).then((res) => {
                // console.log(res)
                if (res.status == 0) {
                    this.setData({
                        citevalue: res.data[0].citename,
                        code: res.data[0].code
                    })
                    this.submitTap()
                    let value = {
                        heatVal: res.data[0].heatVal,
                        id: res.data[0].id,
                    }
                    api.postheatInfo(value).then((res) => {
                        // console.log(res)
                    })
                }else{
                    Toast('无此城市哦！')
                }

            })
        }

    },
    citeTap(e) {
        let value = {
            heatVal: this.data.citeInfo[e.currentTarget.dataset.index].heatVal,
            id: this.data.citeInfo[e.currentTarget.dataset.index].id
        }
        api.postheatInfo(value).then((res) => {
            // console.log(res)
        })
        this.setData({
            citevalue: this.data.citeInfo[e.currentTarget.dataset.index].citename,
            code: this.data.citeInfo[e.currentTarget.dataset.index].code,
            citeInfo: []
        })
    },
    citeChange(e) {
        this.setData({
            citeInfo: [],
            citeShow: true,
            citeName: e.detail
        })
        let value = {
            citename: `${e.detail}%`
        }
        if (e.detail) {
            api.getcite(value).then((res) => {
                // console.log(res)
                this.setData({
                    citeInfo: res.data
                })
            })
        }

    },
    getUserProfile() {
        let that = this
        wx.getUserProfile({
            desc: '获取头像和昵称', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: (res) => {
                this.setData({
                    nickName: res.userInfo.nickName,
                    avatarUrl: res.userInfo.avatarUrl
                })
                wx.setStorage({ //数据缓存方法
                    key: 'nickName', //关键字，本地缓存中指定的key
                    data: res.userInfo.nickName, //缓存微信用户公开信息，
                    success: function () { //缓存成功后，输出提示
                        console.log('写入nickName缓存成功')
                        app.globalData.userInfo.name=wx.getStorageSync('nickName')
                        that.setData({
                            userData: app.globalData.userInfo
                        })
                    },
                    fail: function () { //缓存失败后的提示
                        console.log('写入nickName发生错误')
                        Toast('写入昵称出错~');
                    }
                })
                wx.setStorage({ //数据缓存方法
                    key: 'avatarUrl', //关键字，本地缓存中指定的key
                    data: res.userInfo.avatarUrl, //缓存微信用户公开信息，
                    success: function () { //缓存成功后，输出提示
                        console.log('写入avatarUrl缓存成功')
                        app.globalData.userInfo.headPortrait=wx.getStorageSync('avatarUrl')
                        that.setData({
                            userData: app.globalData.userInfo
                        })
                        // 掉用接口
                        let value = {
                            name: wx.getStorageSync('nickName'),
                            headPortrait: wx.getStorageSync('avatarUrl')
                        }
                        api.updateUserInfo(value).then((res) => {
                            console.log(value)
                            if (res.status == '0') {
                                Toast('修改成功~');
                            } else {
                                Toast('修改失败！');
                            }
                        })
                    },
                    fail: function () { //缓存失败后的提示
                        console.log('写入avatarUrl发生错误')
                        Toast('写入头像出错~');
                    }
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            wxname:wx.getStorageSync('nickName')?wx.getStorageSync('nickName'):'点击获取'
        })
        let dataa = {
            address: app.globalData.userInfo.citecode
        }
        if (dataa.address != null && dataa.address != '') {
            api.postWeatherInfo(dataa).then((res) => {
                let WeatherInfo = res.data.daily
                let date = new Date();
                const hour = date.getHours()
                const minute = date.getMinutes()
                let is_day = WeatherInfo[0].moonrise.split(':')
                if (parseInt(is_day[0]) < parseInt(hour)) {
                    this.setData({
                        is_moonrise: true
                    })
                } else if (parseInt(is_day[0]) == parseInt(hour) && parseInt(is_day[1]) < parseInt(minute)) {
                    this.setData({
                        is_moonrise: true
                    })
                }
                for (let item of WeatherInfo) {
                    if (this.data.is_moonrise) {
                        if (item.textNight.length > 3) {
                            this.setData({
                                is_rollWeaterText: 'rollWeaterText'
                            })
                        }
                    } else {
                        if (item.textDay.length > 3) {
                            this.setData({
                                is_rollWeaterText: 'rollWeaterText'
                            })
                        }
                    }

                }
                this.setData({
                    weatherInfo: WeatherInfo
                })
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})